package cn.agiledata.bank.account.util;

import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.Iterator;

import org.apache.commons.beanutils.BeanUtils;
import org.hibernate.CallbackException;
import org.hibernate.EntityMode;
import org.hibernate.Interceptor;
import org.hibernate.type.Type;

import cn.agiledata.security.db.service.IAvoidJuggleService;

import cn.agiledata.bank.common.util.Constant;
import cn.agiledata.bank.common.util.OperateUtil;
import cn.agiledata.bank.hibernateBean.Account;
import cn.agiledata.bank.hibernateBean.AccountBalance;
import cn.agiledata.bank.hibernateBean.AccountHis;
import cn.agiledata.bank.hibernateBean.AccountLimit;
import cn.agiledata.bank.hibernateBean.AcctBalHis;
import cn.agiledata.bank.hibernateBean.Acctlimithis;
import cn.agiledata.bank.hibernateBean.Transaction;
import cn.agiledata.bank.interest.service.IMutativeInterestService;
import cn.agiledata.bank.myspace.service.IMySpaceService;
import com.vnv.infrastructure.logic.IPBAppFactory;

public class TransactionInterceptor implements Interceptor {
 
	public void onCollectionRecreate(Object arg0, Serializable arg1)
			throws CallbackException {
		// TODO Auto-generated method stub
		
	}

	public void onCollectionRemove(Object arg0, Serializable arg1)
			throws CallbackException {
		// TODO Auto-generated method stub
		
	}

	public void onCollectionUpdate(Object arg0, Serializable arg1)
			throws CallbackException {
		// TODO Auto-generated method stub
		
	}

	public String onPrepareStatement(String arg0) {
		// TODO Auto-generated method stub
		
		return arg0;
	}

	public void afterTransactionBegin(org.hibernate.Transaction arg0) {
		// TODO Auto-generated method stub
		
	}

	public void afterTransactionCompletion(org.hibernate.Transaction arg0) {
		// TODO Auto-generated method stub
		
	}

	public void beforeTransactionCompletion(org.hibernate.Transaction arg0) {
		// TODO Auto-generated method stub
		
	}

	public void afterTransactionBegin(Transaction arg0) {
		// TODO Auto-generated method stub
		
	}

	public void afterTransactionCompletion(Transaction arg0) {
		// TODO Auto-generated method stub
		
	}

	public void beforeTransactionCompletion(Transaction arg0) {
		// TODO Auto-generated method stub
		
	}

	public int[] findDirty(Object arg0, Serializable arg1, Object[] arg2,
			Object[] arg3, String[] arg4, Type[] arg5) {
		// TODO Auto-generated method stub
		
		return null;
	}

	public Object getEntity(String arg0, Serializable arg1)
			throws CallbackException {
		// TODO Auto-generated method stub
		
		return null;
	}

	public String getEntityName(Object arg0) throws CallbackException {
		// TODO Auto-generated method stub
		
		return null;
	}

	public Object instantiate(String arg0, EntityMode arg1, Serializable arg2)
			throws CallbackException {
		// TODO Auto-generated method stub
		
		return null;
	}

	public Boolean isTransient(Object arg0) {
		// TODO Auto-generated method stub
		
		return null;
	}

	public void onDelete(Object arg0, Serializable arg1, Object[] arg2,
			String[] arg3, Type[] arg4) throws CallbackException {
		// TODO Auto-generated method stub
		

	}
	public boolean onFlushDirty(Object arg0, Serializable arg1, Object[] arg2,
			Object[] arg3, String[] arg4, Type[] arg5) throws CallbackException {
		// TODO Auto-generated method stub
		
		if ((arg0 instanceof Transaction)) {
			if(((Transaction)arg0).getActionType().getId().toString().equals(Constant.ACTION_TYPE_C)) { //账户间交易
				if(((Transaction)arg0).getTradeType().equals(Constant.TRADE_TYPE_ASSURE)) {//担保交易
					if(((Transaction)arg0).getTransStatus().equals(Constant.TRANS_STATUS_TRANSACTIONSUCCESS) ||
						((Transaction)arg0).getTransStatus().equals(Constant.TRANS_STATUS_REFUNDSUCC) ||
						((Transaction)arg0).getTransStatus().equals(Constant.TRANS_STATUS_TRANSACTIONCLOSE)
						) { //交易成功,退款成功,交易取消
						((Transaction)arg0).setEndDate(new Date());
					}
				} else { //非担保交易
					((Transaction)arg0).setEndDate(new Date());
				}
			}
			processAvoidJuggle(arg0,arg4,arg2); //处理篡改
		}
		else if (arg0 instanceof Account) {
			Account account = (Account) arg0;
			AccountHis acctHis = new AccountHis();
			setAcctHis(account, acctHis);
			IMySpaceService service = (IMySpaceService) IPBAppFactory
					.getObj(IMySpaceService.MGR_ID);
			service.saveAcctHis(acctHis);
			processAvoidJuggle(arg0,arg4,arg2); //处理篡改
		} 
		else if (arg0 instanceof AccountBalance) {// 修改积数
			this.getInterestAccumulatorService().setAccumulation((AccountBalance) arg0,
					arg1, arg2, arg3, arg4, arg5);
			processAvoidJuggle(arg0,arg4,arg2); //处理篡改
		}
		else if (arg0 instanceof AccountLimit) {
			AccountLimit al = (AccountLimit) arg0;
			Acctlimithis acctHis = new Acctlimithis();
			try {
				BeanUtils.copyProperties(acctHis, al);
			} catch (IllegalAccessException e) {		
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				e.printStackTrace();
			}
			IMySpaceService service = (IMySpaceService) IPBAppFactory
					.getObj(IMySpaceService.MGR_ID);
			service.saveAcctLimitHis(acctHis);
		} 
		return false;
	}

	public boolean onLoad(Object arg0, Serializable arg1, Object[] arg2,
			String[] arg3, Type[] arg4) throws CallbackException {
		// TODO Auto-generated method stub
		if (arg0 instanceof AcctBalHis) {

			for (int i = 0; i < arg3.length; i++) {
				if (arg3[i].equals("amount")) {
					arg2[i] = OperateUtil.parseDec(
							new Double((String) arg2[i]), "##,###,###.00");
				}
			}
		}
		return false;
	}

	public boolean onSave(Object arg0, Serializable arg1, Object[] arg2,
			String[] arg3, Type[] arg4) throws CallbackException {
		// TODO Auto-generated method stub
		if (arg0 instanceof Transaction) {
			((Transaction) arg0).setInsideStatus("N");
			//((Transaction) arg0).setEndDate(new Date());	edit by cjh
			for (int i = 0; i < arg3.length; i++) {
				//if (arg3[i].equals("endDate")) {
				//	arg2[i] = new Date();
				//}
				if (arg3[i].equals("insideStatus")) {
					arg2[i] = "N";
				}
			}
			processAvoidJuggle(arg0,arg3,arg2); //处理篡改
		}
		
		if(arg0 instanceof AccountBalance) {
			processAvoidJuggle(arg0,arg3,arg2); //处理篡改
			return true;
		}
		
		if(arg0 instanceof Account) {
			processAvoidJuggle(arg0,arg3,arg2); //处理篡改
			return true;			
		}
		
		return false;
	}

	public void postFlush(Iterator arg0) throws CallbackException {
		// TODO Auto-generated method stub
	}

	public void preFlush(Iterator arg0) throws CallbackException {
		// TODO Auto-generated method stub
		
	}

	public void setAcctHis(Account acct, AccountHis acctHis) {
		try{
		BeanUtils.copyProperties(acctHis,acct);}
		catch(Exception e){e.printStackTrace();}
		if (acct.isSystem()) {
			acctHis.setUpdateUserSystem(acct.getUpdateUser());
		} else {
			acctHis.setUpdateUser(acct.getUpdateUser());
		}
		acctHis.setCardPhoto(null);
		acctHis.setPhoto(null);
		acctHis.setCardPhotoBack(null);
		acctHis.setTempCardPhoto(null);
		acctHis.setTempCardPhotoBack(null);
	}
	private IMutativeInterestService interestAccumulatorService = null;
	public IMutativeInterestService getInterestAccumulatorService() {
		if(this.interestAccumulatorService==null){
			this.interestAccumulatorService=(IMutativeInterestService) IPBAppFactory
			.getObj("interestAccumulatorService");
		}
		return interestAccumulatorService;
	}


	public void setInterestAccumulatorService(
			IMutativeInterestService interestAccumulatorService) {
		this.interestAccumulatorService = interestAccumulatorService;
	}
	/**
	 * 处理篡改
	 * @param entity
	 * @param propertyNames
	 * @param state
	 * @return
	 */
	public void processAvoidJuggle(Object entity,Object[] propertyNames,Object[] state) {
		for (int i = 0; i < propertyNames.length; i++) {
			if (propertyNames[i].equals("encryptCode")) {
				state[i] = getAvoidJuggleService().parseEncrCode(entity);
			}
		}
	}
	
	private IAvoidJuggleService avoidJuggleService;
	public IAvoidJuggleService getAvoidJuggleService() {
		if(this.avoidJuggleService == null) {
			return (IAvoidJuggleService)IPBAppFactory.getObj("AvoidJuggleService");
		} else {
			return this.avoidJuggleService;
		}
	}

	public void setAvoidJuggleService(IAvoidJuggleService avoidJuggleService) {
		this.avoidJuggleService = avoidJuggleService;
	}
	
}


